package com.zrt.pad.network.response;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;

import com.zrt.pad.util.DESUtil;

public class RespResult {

	public String errorInfo;
	public int succedFlag;
	public JSONObject returnInfo;
	public JSONArray returnArray;

	private RespResult() {
		errorInfo = "";
		succedFlag = -1;
		returnInfo = new JSONObject();
	}

	public static RespResult initRespResult(String json) {		
		RespResult rr = new RespResult();
		try {
			JSONObject jobj = new JSONObject(json);
			//正确返回
			if(jobj.getBoolean("succeed"))
			{
				String data = jobj.getString("data");	
				JSONObject myJsonObject = new JSONObject(data);
				WebReturn rtn = new WebReturn();
			    rtn = rtn.initWithJSON(myJsonObject);
			    if(rtn.getSucceed())
			    {
			    	rr.succedFlag = rtn.getSuccedFlag();
					Log.e("succeedFlag", rr.succedFlag + "");
					if (rr.succedFlag == 1) {
						
						if(rtn.getData() !=null )
						{
							Object obj = rtn.getData();
							if (obj instanceof JSONObject) {
								rr.returnInfo = (JSONObject) obj;
							} else if (obj instanceof JSONArray) {
								rr.returnArray = (JSONArray) obj;
							}
						}
						
//						String encryptMsg = jobj.getString("returnInfo");
//						if (encryptMsg != null && !encryptMsg.equals("")) {
//							String decryptMsg = DESUtil.instance
//									.getDecString(encryptMsg);
//							Object obj = new JSONTokener(decryptMsg).nextValue();
//							if (obj instanceof JSONObject) {
//								rr.returnInfo = (JSONObject) obj;
//							} else if (obj instanceof JSONArray) {
//								rr.returnArray = (JSONArray) obj;
//							}
//						}
					}
			    }
			    else
			    {
			    	rr.succedFlag = -1;
					rr.errorInfo = rtn.getMessage();
			    }
			}
			else
			{
				rr.succedFlag = -1;
				rr.errorInfo = jobj.getString("message");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rr;
	}

	public boolean isSucceed() {
		return 1 == succedFlag;
	}

	public String getErrorInfo() {
		return errorInfo;
	}
}
